﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EF;
using EntityFramework.Patterns;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Blog> blogs = GetAllBlog();
            Console.WriteLine("before UpdateAndDelete:" + blogs.Count.ToString());
            AddBolg(3,"blog3");
          //  UpdateAndDelete();
            Console.WriteLine("Press any key to exit...");
        }

        public  static List<Blog> GetAllBlog()
        {
            List<Blog> blogs = new List<Blog>();
            using (var ctx = new MyDbContext())
            {
                DbContextAdapter adapter = new DbContextAdapter(ctx);
                IRepository<Blog> productRepo = new Repository<Blog>(adapter);
                IEnumerable<Blog> varBlogs = productRepo.GetAll();
                blogs = varBlogs.ToList();
            }
            return blogs;
        }


        public static void AddBolg(int id, string name)
        {
            Blog newBlog = new Blog() { BlogId = id, Name = name };
            using (var ctx = new MyDbContext())
            {
                DbContextAdapter adapter = new DbContextAdapter(ctx);
                IRepository<Blog> blogRepo = new Repository<Blog>(adapter);
                IUnitOfWork unitOfWork = new UnitOfWork(adapter);
                blogRepo.Insert(newBlog);
                unitOfWork.Commit();
            }
        }
        public static void UpdateAndDelete()
        {
            using (var ctx = new MyDbContext())
            {
                DbContextAdapter adapter = new DbContextAdapter(ctx);
                IRepository<Blog> productRepo = new Repository<Blog>(adapter);
                IUnitOfWork unitWork = new UnitOfWork(adapter);
                Blog blog1 = productRepo.First(t => t.BlogId == 1);
                blog1.Name = "blog1_update";
                Blog blog2 = productRepo.First(t => t.BlogId == 2);
                productRepo.Delete(blog2);
                unitWork.Commit();
            } 
        }
    }
}
